package com.controller;

import com.model.*;
import com.response.Response;
import com.service.*;
import com.util.PageBean;
import com.util.removeHTML;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
@RequestMapping("/api/category")
public class CategoryController{
	
	@Resource
	private CategoryService categoryService;
	
	//任务分类列表
	@RequestMapping(value="/list")
	@CrossOrigin
	public Response<List<Category>> list(@RequestBody Category category, @RequestParam Integer currentPage,
			HttpServletRequest req) throws Exception {
		int pageSize = Integer.parseInt(req.getParameter("pageSize")); //每页显示记录数
		int offset = (currentPage - 1) * pageSize; //当前页开始记录
		int counts = 0; //总记录数
		PageBean page = new PageBean(offset, pageSize); //分页对象
		//查询记录总数
		counts = categoryService.getCount(category);
		//获取当前页记录
		List<Category> categoryList = categoryService.queryCategoryList(category, page);

		int page_count = counts % PageBean.PAGE_IETM == 0 ? counts / PageBean.PAGE_IETM
				: counts / PageBean.PAGE_IETM + 1; //总页数
		return Response.success(categoryList, counts, page_count);
	}
	
	// 任务分类列表
	@RequestMapping(value = "/list2")
	@CrossOrigin
	public Response<List<Category>> list2( Category category, @RequestParam Integer currentPage,
			HttpServletRequest req) throws Exception {
		int pageSize = Integer.parseInt(req.getParameter("pageSize")); // 每页显示记录数
		int offset = (currentPage - 1) * pageSize; // 当前页开始记录
		int counts = 0; // 总记录数
		PageBean page = new PageBean(offset, pageSize); // 分页对象
		// 查询记录总数
		counts = categoryService.getCount(category);
		// 获取当前页记录
		List<Category> categoryList = categoryService.queryCategoryList(category, page);

			String f = req.getParameter("f");
			if (f != null && f.equals("5")) {
					Category category1 = new Category();
					category1.setTname("全部");
					category1.setTid(0);
					categoryList.add(0, category1);
				}

		int page_count = counts % PageBean.PAGE_IETM == 0 ? counts / PageBean.PAGE_IETM
				: counts / PageBean.PAGE_IETM + 1; // 总页数
		return Response.success(categoryList, counts, page_count);
	}
        
	//添加任务分类
	@ResponseBody
	@PostMapping(value = "/add")
	@CrossOrigin
	public Response add(@RequestBody Category category, HttpServletRequest req) throws Exception {
		try {
			//判断用户名是否存在
            Category category1 = new Category();
            category1.setTname(category.getTname());
            List<Category> categoryList = categoryService.queryCategoryList(category1, null);
            if (categoryList.size() > 0) {
                return Response.error(201, "类别名称已存在，请重新输入");
            }
            else
            {
                categoryService.insertCategory(category); //添加
            }
   
			} catch (Exception e) {
			return Response.error();
		}
		return Response.success();
	}
    
	//删除任务分类
	@ResponseBody
	@PostMapping(value = "/del")
	@CrossOrigin
	public Response del(HttpServletRequest req) throws Exception {
		try {
			int id = Integer.parseInt(req.getParameter("id"));
			categoryService.deleteCategory(id); //删除
			} catch (Exception e) {
			return Response.error();
		}
		return Response.success();
	}
	
	//修改任务分类
	@ResponseBody
	@PostMapping(value = "/update")
	@CrossOrigin
	public Response update(@RequestBody Category category, HttpServletRequest req) throws Exception {
		try {
			categoryService.updateCategory(category); //修改
			} catch (Exception e) {
			return Response.error();
		}
		return Response.success();
	}
	
	//返回任务分类详情
	@ResponseBody
	@PostMapping(value = "/get")
	@CrossOrigin
	public Response get(HttpServletRequest req) throws Exception {
		try {
			int id = Integer.parseInt(req.getParameter("id"));
			Category category=categoryService.queryCategoryById(id); //根据ID查询
			return Response.success(category);
			} catch (Exception e) {
			return Response.error();
		}
       
	}
    
}

